<?php
namespace app\admins\controller;
use think\Controller;
use Util\data\Sysdb;



class Menu extends BaseAdmin
{
	public function index(){
		$pid=(int)input('get.pid');
		$data['lists']=$this->db->table('admin_menus')->where(array('pid'=>$pid))->lists();

		//返回上级
		$backid=0;
		if($pid>0){
			$parent=$this->db->table('admin_menus')->where(array('mid'=>$pid))->item();
			$backid=$parent['pid'];
		}
		$this->assign('pid',$pid);
		$this->assign('backid',$backid);
		$this->assign('data',$data);
		return $this->fetch();
	}

	//保存菜单	
	public function save(){

		$pid=(int)input('post.pid');
		$ord=input('post.ord/a');
		$title=input('post.title/a');
		$controller=input('post.controller/a');
		$method=input('post.method/a');
		$ishidden=input('post.ishidden/a');
		$status=input('post.status/a');//tp传数组方式

		foreach ($ord as $key => $value) {
			//新增
			$data['pid']=$pid;
			$data['ord']=$value;
			$data['title']=$title[$key];
			$data['controller']=$controller[$key];
			$data['method']=$method[$key];
			$data['ishidden']=isset($ishidden[$key])? 1:0;
			$data['status']=isset($status[$key])? 1: 0;  


			if($key==0&& $data['title']){
				$this->db->table('admin_menus')->insert($data);
			}
			if($key>0){
				if($data['title']==''&$data['controller']==''&$data['method']==''){
					//删除
					$this->db->table('admin_menus')->where(array('mid'=>$key))->delete();
				}else{
					//修改
					$this->db->table('admin_menus')->where(array('mid'=>$key))->update($data);
				}
			}

		}
		exit(json_encode(array('code'=>0,'msg'=>'保存成功')));


	}


}
